// circle/publish/publish.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mylocation: '我的位置',
    qzimgID: [],
    quimgup: [],
    node : ''
  },

  location:function(){
    var that = this;
    // if (islocat) {
      wx.getLocation({
          type: 'wgs84',
          success: function(res) {
            var latitude = res.latitude
            var longitude = res.longitude
            var speed = res.speed
            var accuracy = res.accuracy
            // console.log(latitude + "****" + longitude + "****" + speed + "****" + accuracy)
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
            })
          }
        }),
        wx.chooseLocation({
          success: function(res) {
            that.setData({
              latitude: res.latitude,
              longitude: res.longitude,
              mylocation: res.name + "(" + res.address + ")",
            })
          }
        })
    // }
  },
  upImg: function() {
    var that = this;
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        wx.showLoading({
          title: '上传中',
        })
        const filePath = res.tempFilePaths[0]
        // 上传图片
        const cloudPath = new Date().getTime() + filePath.match(/\.[^.]+?$/)[0];
        console.log(res);
        console.log(cloudPath);
        console.log(filePath);
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res);
            that.getimgpath(res.fileID);
            var oldId = that.data.qzimgID;
            oldId.push(res.fileID);
            that.setData({
              qzimgID: oldId
            });
            console.log(that.data.qzimgID)
            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },
  getimgpath: function(path) {
    // console.log(1)
    // console.log(path)
    var that = this;
    wx.cloud.getTempFileURL({
      fileList: [path],
      success: res => {
        // get temp file URL
        console.log(res.fileList[0].tempFileURL)
        var oldpath = that.data.quimgup;
        oldpath.push(res.fileList[0].tempFileURL);
        that.setData({
          quimgup: oldpath
        });
      },
      fail: err => {
        // handle error
      }
    })
  },
  formSubmit: function(e) {
    var data = {};
    var db = wx.cloud.database();
    var that = this;
    data.node = e.detail.value.node;
    data.imgs = this.data.qzimgID;
    data.imgnum = this.data.quimgup.length;
    console.log(e.detail.value.node)
    wx.getUserInfo({
      success: function(res) {
        var userInfo = res.userInfo
        data.nickName = userInfo.nickName;
        data.gender = userInfo.gender //性别 0：未知、1：男、2：女
        data.avatarUrl = userInfo.avatarUrl;
        console.log(data);
        db.collection('news').add({
          data: data,
          success: function (res) {
            wx.showToast({
              title: '哎呀，你添加成功了',
            })
            that.onLoad();
            that.setData({
              node:'',
              quimgup:[],
              qzimgID:[]
            })
          },
          fail: console.error
        })
      }
    })
    
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})